﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.IO.Compression;
using System.Net.Http;
using System.Threading.Tasks;
using System.Net.Http.Headers;
using System.Diagnostics;

namespace PublishRepository
{
	public class Utility
	{
		public static void ZipWeb(string directory, string fileName = "build.zip")
		{
			DirectoryInfo from = new DirectoryInfo(directory);
			string zipFileName = Path.Combine(directory, fileName);
			if (File.Exists(zipFileName))
			{
				File.Delete(zipFileName);
			}

			List<string> except = new List<string>() { "ThirdParty", "Web.config", "local_setting", "index.html", fileName };
			using (FileStream zipToOpen = new FileStream(zipFileName, FileMode.Create))
			{
				using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Create))
				{
					foreach (FileInfo file in from.AllFilesAndFolders().Where(o => o is FileInfo).Cast<FileInfo>())
					{
						if (except.Any(c => file.FullName.Contains(c)))
						{
							continue;
						}
						var relPath = file.FullName.Substring(from.FullName.Length + 1);
						ZipArchiveEntry readmeEntry = archive.CreateEntryFromFile(file.FullName, relPath);
					}
				}
			}
		}

		public static void ZipApi(string directory, string fileName = "bin.zip")
		{
			DirectoryInfo from = new DirectoryInfo(directory);
			string zipFileName = Path.Combine(directory, fileName);
			if (File.Exists(zipFileName))
			{
				File.Delete(zipFileName);
			}
			List<string> except = new List<string>() { fileName };
			using (FileStream zipToOpen = new FileStream(zipFileName, FileMode.Create))
			{
				using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Create))
				{
					foreach (FileInfo file in from.AllFilesAndFolders().Where(o => o is FileInfo).Cast<FileInfo>())
					{
						if (except.Any(c => file.FullName.Contains(c)))
						{
							continue;
						}
						if (file.LastWriteTime.Date == DateTime.Now.Date && file.Extension.Contains("dll"))
						{
							var relPath = file.FullName.Substring(from.FullName.Length + 1);
							ZipArchiveEntry readmeEntry = archive.CreateEntryFromFile(file.FullName, relPath);
						}
					}
				}
			}
		}

		public static async Task<string> Post(string url, string filePath)
		{
			using (var form = new MultipartFormDataContent())
			{
				using (var stream = File.OpenRead(filePath))
				{
					using (var streamContent = new StreamContent(stream))
					{
						form.Add(new ByteArrayContent(File.ReadAllBytes(filePath)), "build", "build.zip");

						using (var client = new HttpClient())
						{
							var response = await client.PostAsync(url, form);
							return await response.Content.ReadAsStringAsync();
						}
					}
				}
			}
		}

		public static void RunWebTask(string taskLocation)
		{
			string fileName = "gulp";
			string arguments = "serve";
			if (File.Exists(Path.Combine(taskLocation, "Gruntfile.js")))
			{
				fileName = "grunt";
				arguments = "debug";
			}

			var process = new Process
			{
				StartInfo = new ProcessStartInfo
				{
					FileName = fileName,
					Arguments = arguments,
					UseShellExecute = true,
					WindowStyle = ProcessWindowStyle.Hidden,
					RedirectStandardOutput = false,
					CreateNoWindow = true,
					WorkingDirectory = taskLocation
				}
			};
			process.Start();
			process.WaitForExit();
		}
	}

}

public static class FileExtensions
{
	public static IEnumerable<FileSystemInfo> AllFilesAndFolders(this DirectoryInfo dir)
	{
		foreach (var f in dir.GetFiles())
			yield return f;
		foreach (var d in dir.GetDirectories())
		{
			yield return d;
			foreach (var o in AllFilesAndFolders(d))
				yield return o;
		}
	}
}
